package cn.edu.cqyz.wisdom.service.impl;

import cn.edu.cqyz.wisdom.entity.R;
import cn.edu.cqyz.wisdom.entity.Session;
import cn.edu.cqyz.wisdom.mapper.SessionMapper;
import cn.edu.cqyz.wisdom.service.ISessionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 历史会话表 服务实现类
 * </p>
 *
 * @author CDHong.it
 * @since 2024-12-18
 */
@Service
public class SessionServiceImpl extends ServiceImpl<SessionMapper, Session> implements ISessionService {

    @Autowired
    public SessionMapper sessionMapper;

    /**
     * 创建会话
     */
    @Override
    public R createSession(Session session) {
        Session newSession = new Session();
        newSession.setUserId(session.getUserId());
        newSession.setSessionName(session.getSessionName());
        try {
            sessionMapper.insert(newSession);
            return R.data(newSession);
        } catch (Exception e) {
            return R.error("网络异常!");
        }
    }

    /**
     * 获取会话
     */
    @Override
    public List<Session> findAllSession(Integer id) {
        QueryWrapper<Session> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        wrapper.orderByDesc("id");
        try {
            return sessionMapper.selectList(wrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 编辑名称
     */
    @Override
    public R updateName(Session session) {
        Session newSession = new Session();
        newSession.setId(session.getId());
        newSession.setSessionName(session.getSessionName());
        try {
            sessionMapper.updateById(newSession);
            return R.ok("修改成功!");
        } catch (Exception e) {
            return R.error("网络异常!");

        }
    }


}
